package com.apes.crm.masterControl;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import com.apes.framework.query.SqlService;
import com.apestech.framework.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Title: customerControlDao
 * @Description:
 * @author: ZhengZeXian
 * @create: 2021/1/27 10:06
 */
@Component
@SuppressWarnings("all")
public class customerControlDao extends ApesDao {

    @Autowired
    SqlService sqlService;

    /**
     * 查询OA
     * @param param
     * @return
     */
    public List<Map> queryOAByParam(JSONObject param){
        JSONArray array = sqlService.queryId("oa2.queryAllProcess", new com.apestech.framework.json.JSONObject(param));
        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
        return maps;
    }

    /**
     * 查询需签合同提醒
     * @param param
     * @return
     */
    public List<Map> queryNeedContract(JSONObject param){
        JSONArray array = sqlService.queryId("contract2.query", new com.apestech.framework.json.JSONObject(param));
        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
        return maps;
    }

    /**
     * 查询续签合同提醒
     * @param param
     * @return
     */
    public List<Map> queryRenewContract(JSONObject param){
        JSONArray array = sqlService.queryId("renewContract.query", new com.apestech.framework.json.JSONObject(param));
        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
        return maps;
    }

//    /**
//     * 根据发起部门（行政部门）获取OA
//     * @param branchId
//     * @return
//     */
//    public List<Map> queryOA(String branchId){
//        JSONObject param = new JSONObject();
//        param.put("processToDoDepartment",branchId);
//        param.put("finished",0);
//        JSONArray array = sqlService.queryId("oa2.queryAllProcess", new com.apestech.framework.json.JSONObject(param));
//        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
//        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
//        return maps;
//    }

//    /**
//     * 根据人员 获取OA
//     * @param userId
//     * @return
//     */
//    public List<Map> queryOAByUserId(List<String> userId){
//        JSONObject param = new JSONObject();
//        param.put("users",userId);
//        param.put("finished",0);
//        JSONArray array = sqlService.queryId("oa2.queryAllProcess", new com.apestech.framework.json.JSONObject(param));
//        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
//        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
////        maps = maps.stream().filter(distinctByKey(b -> b.get("TASKID"))).collect(Collectors.toList());  //根据任务ID去重复的
//        return maps;
//    }
//
//    /**
//     * 根据人员 获取OA
//     * @param userId
//     * @return
//     */
//    public List<Map> queryOAMultiple(List<String> userId,String documentType,String statusName){
//        JSONObject param = new JSONObject();
//        param.put("users",userId);
//        param.put("name",documentType);
//        param.put("curTaskName",statusName);
//        param.put("finished",0);
//        JSONArray array = sqlService.queryId("oa2.queryAllProcess", new com.apestech.framework.json.JSONObject(param));
//        com.alibaba.fastjson.JSONArray jsonArray = JSONObject.parseArray(String.valueOf(array));
//        List<Map> maps = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
////        maps = maps.stream().filter(distinctByKey(b -> b.get("TASKID"))).collect(Collectors.toList());  //根据任务ID去重复的
//        return maps;
//    }

    static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object,Boolean> seen = new ConcurrentHashMap<>(); return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
